package com.gc.baselib.view;


import android.content.Context;
import android.util.AttributeSet;

import androidx.appcompat.widget.AppCompatImageView;

/**
 * 点击透明度改变为60%
 */
public class AlphaImageView extends AppCompatImageView {

    private boolean touchEffect = true;

    public AlphaImageView(Context context) {
        super(context);
    }

    public AlphaImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public AlphaImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public void setPressed(boolean pressed) {
        updateView(pressed);
        super.setPressed(pressed);
    }

    /**
     * 根据是否按下去来刷新bg和src
     * @param pressed
     */
    private void updateView(boolean pressed) {
        //如果没有点击效果
        if (!touchEffect) {
            return;
        }//end if
        if (pressed) {//点击
            this.setDrawingCacheEnabled(true);
            if(this.getBackground()==null){
                if(this.getDrawable()!=null){
                    this.getDrawable().setAlpha((int)(0.6*255));
                }
            }else{
                this.getBackground().setAlpha((int)(0.6*255));
            }

        } else {//未点击
            if(this.getBackground()==null){
                if(this.getDrawable()!=null){
                    this.getDrawable().setAlpha(255);
                }
            }else{
                this.getBackground().setAlpha(255);
            }

        }
    }
}